package cn.gok.manage.service.impl;

import cn.gok.dao.CategoryMapper;
import cn.gok.dao.ProductMapper;
import cn.gok.manage.service.IProductService;
import cn.gok.manage.web.vo.AddressVO;
import cn.gok.manage.web.vo.ProductVO;
import cn.gok.manage.web.vo.ShippingVO;
import cn.gok.pojo.Category;
import cn.gok.pojo.Product;
import cn.gok.pojo.Shipping;
import cn.gok.util.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author: 鸭鸭
 * @ClassName:ProductServiceImpl
 * @Package:cn.gok.gokhappymall.manage.service.impl
 * @Description:
 * @date: 2021/8/3
 * @time: 16:36
 */
@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    ProductMapper productMapper;

    @Autowired CategoryMapper categoryMapper;

    ProductVO productVO = null;
    @Override
    public ResultData getAllProducts(int pageNum, int pageSize) {
        List<Product> products = productMapper.queryAllProducts();
        for(Product product : products) {
            System.out.println(product);
        }
        productVO = new ProductVO();
        productVO.setProducts(products);
        if (products != null) {
            return ResultData.createData(productVO);
        }return ResultData.fail("用户未登录请登录");
    }

    @Override
    public ResultData getProductsByIdAndName(Integer id, String name) {
        List<Product> products = productMapper.queryProductsByIdAndName(id, name);
        for (Product product:products){
            System.out.println(product);
        }
        productVO = new ProductVO();
        productVO.setProducts(products);

        if (products != null) {
            return ResultData.createData(productVO);
        }return ResultData.fail("用户未登录请登录");
    }

    @Override
    public ResultData getProducttById(Integer id) {
        Product product = productMapper.queryProductById(id);
        System.out.println(product);

        if (product != null){
            return ResultData.createData(product);
        }
        return ResultData.fail("没有权限");

    }

    @Override
    public ResultData updateProductStatus(Integer id, Integer status) {
        int product = productMapper.updateProductStatus(id, status);
        System.out.println(product);

        if (product != 0){
            return ResultData.success("修改商品状态成功");
        }
        return ResultData.fail("修改商品状态失败");

    }

    @Override
    public ResultData insertProduct(Product product) {
        int insert = productMapper.insert(product);
        System.out.println(insert);
        if (insert != 0){
            return ResultData.success("新增商品成功");
        }
            return ResultData.fail("新增商品失败");
    }

    @Override
    public ResultData updatePhoto(MultipartFile mf, String desc, HttpServletRequest httpServletRequest) {
        String realpath = "D:/java web/项目/gok_mall/src/main/resources/static/images/";
        File file = new File(realpath);
        if (!file.exists()) {
            file.mkdir();
        }
        //得到后缀
        String fileName = mf.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //新名
        String newFileName = UUID.randomUUID().toString() + suffix;
        String uri = newFileName;
        String url = "http:localhost:8080/images/" + newFileName;
        System.out.println(url);
        AddressVO addressVO = new AddressVO(uri, url);
        file = new File(realpath + File.separator + newFileName);
        try {
            mf.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //需要输入id来获取需要修改的商品的图片
        int id = 26;
        int num = productMapper.updatPhotoById(id, realpath + File.separator + newFileName);
        //if (num>0) System.out.println("success!");
        return ResultData.createData(addressVO);

    }
    @Override
    public ResultData selectProductByKeyWord(String keyword, int categoryId,int pageNum,int pageSize,String orderByName) {
        PageHelper.startPage(pageNum, pageSize, true);
        //分割orderBy
        String orderBy = orderByName.replace('_',' ');
        System.out.println(orderBy);
        List<Product> list = productMapper.selectByKeyWord(keyword,categoryId,orderBy);
        List<Integer> ids = new ArrayList<>();
        for(Product product : list){
            Category category = categoryMapper.selectByPrimaryKey(product.getCategoryId());
            while(category.getParentId()!=0){
                Category category1 = categoryMapper.selectByPrimaryKey(category.getParentId());
                ids.add(category1.getId());
            }
        }
        for(int id :ids){
            Product product2 = productMapper.selectByPrimaryKey(id);
            list.add(product2);
        }
        PageInfo<Product> pageInfo = new PageInfo<Product>(list);
        ProductVO productVO = new ProductVO();
        productVO.setPageNum(pageNum);
        productVO.setPageSize(pageSize);
        productVO.setSize(pageInfo.getSize());
        productVO.setOrderBy(orderByName);
        productVO.setStartRow(pageInfo.getStartRow());
        productVO.setEndRow(pageInfo.getEndRow());
        productVO.setTotal(pageInfo.getTotal());
        productVO.setPages(pageInfo.getPages());
        productVO.setProducts(list);
        productVO.setFirstPage(pageInfo.getNavigateFirstPage());
        productVO.setPrePage(pageInfo.getPrePage());
        productVO.setNextPage(pageInfo.getNextPage());
        productVO.setLastPage(pageInfo.getNavigateLastPage());
        if(pageInfo.getPageNum()==1){
            productVO.setIsFirstPage(true);
            productVO.setHasPreviousPage(false);
        }else{
            productVO.setIsFirstPage(false);
            productVO.setHasPreviousPage(true);
        }
        if(pageInfo.getNavigateLastPage()==pageInfo.getPageNum()) {
            productVO.setIsLastPage(true);
            productVO.setHasNextPage(false);
        }else{
            productVO.setIsLastPage(false);
            productVO.setHasNextPage(true);
        }
        productVO.setNavigatePages(pageInfo.getNavigatePages());
        productVO.setNavigatepageNums(pageInfo.getNavigatepageNums());

        return ResultData.createData(productVO);

    }
    @Override
    public ResultData insert_Product(Product product) {
        if (product.getId()==null){
            int num=productMapper.insert(product);
            if (num==1){
                return ResultData.createData("新增产品成功");
            }
            return ResultData.createData("新增产品失败");
        }
        if ( product!=null){
            int num=productMapper.updateByPrimaryKey(product);
            if (num==1){
                return ResultData.createData("修改产品成功");
            }
            return ResultData.createData("修改产品失败");
        }
        return ResultData.createData("产品修改/更新失败");

    }


}
